/*!
 * 列表对象.
 * ecp.qzz.list.js
 * 创建者：QZZ
 * 日期：2014-03-01
 */
define(["qzz.object"], function() {
	var qzz = window.qzz;
	/**
	 * @class List
	 */
	qzz.list = qzz.Class(qzz.object,/**@lends List */ {
		/**
		 * 创建函数.
		 */
		_create:function(){
		    this.base();
		    this.className = "List";
		    this.logInfo("List.create");
		    //过滤及状态
		    this.filtered = false;
		    this.filter = {};
		    //原始数列
		    this.datas = [];
		    this.selectIndex = -1;
		    //过滤数列
		    this.fDatas = [];
		    this.fselectIndex = -1;
		    //字段列
		    this.idColName = "id";
		    this.textColName = "text";
	    },
	    /**
	     * 过滤处理.
	     * @ignore
	     */
	    _refreshFilter:function() {
	    	//过滤处理
	    	if(this.filtered) {
		    	this.fDatas = [];
		    	var txt = ""
		    	for(var i = 0, j = 0, len = this.datas.length; i < len; i++) {
		    		var cd = this.datas[i];
		    		if(cd._filter === false) {
		    			this.fDatas[j++] = cd;
		    			continue;
		    		}
		    		txt = cd[this.textColName] + "";
		    		var ftxt = txt.replace(/,/g,"");
		    		if(isNaN(ftxt)) {
		    			ftxt = "";
		    		}
		    		for(var key in this.filter) {
			    		if(txt.indexOf(key) >= 0 || ftxt !== "" && ftxt.indexOf(key) >= 0) {
			    			this.fDatas[j++] = cd;
			    			break;
			    		}
		    		}
		    	}
	    	} else {
	    		this.fDatas = [];
		    	for(var i = 0, len = this.datas.length; i < len; i++) {
			    	this.fDatas[i] = this.datas[i];
		    	}
	    	}
	    },
	    /**
	     * 设置过滤条件.
	     * @param {string} text 条件
	     * @param {Boolean} done 完成操作后是否刷新
	     * @example
	     *    list.setFilter("123");
	     */
	    addFilter:function(text, done){
	    	if(typeof text === "string") {
	    		if(this.filter[text] != null) {
	    		    return;
	    		} else {
	    			this.filter[text] = true;
	    		}
	    	} else if(this.isArray(text)) {
	    		for(var i = 0, len = text.length; i < len; i++) {
	    			this.filter[text] = true;
	    		}
	    	}
	    	if(done != false) {
	    		this.filtered = true;
		    	this._refreshFilter();
		    	
	    	}
	    },
	    /**
	     * 排序处理.
	     */
	    sort:function(key, desc) {
	    	if(key == null && key === "") {
	    		key = this.idColName;
	    	}
	    	if(desc == null) {
	    		desc = false;
	    	}
	    	this.cashJson = {};
	    	for(var i = 0, len = this.datas.length; i < len; i++) {
	    		for(var j = i + 1;j < len; j++) {
	    			var a = this.datas[i];
	    			if(typeof a == "object") {
	    				a = a[key];
	    			}	    			
	    			var b = this.datas[j];
	    			if(typeof b == "object") {
	    				b = b[key];
	    			}
	    			if(desc) {
		    			if(this.compare(a, b, this.cashJson) < 0) {
		    				var tmp = this.datas[i];
		    				this.datas[i] = this.datas[j];
		    				this.datas[j] = tmp;
		    			}
	    			} else {
	    				if(this.compare(a, b, this.cashJson) > 0) {
		    				var tmp = this.datas[i];
		    				this.datas[i] = this.datas[j];
		    				this.datas[j] = tmp;
		    			}
	    			}
	    		}
	    	}
	    	this._refreshFilter();
	    },
	    /**
	     * 执行过滤
	     * @param {Boolean} [done = true] 
	     */
	    doFilter:function(done) {
	    	if(done == null) {
	    		done = true;
	    	}
	    	this.filtered = done;
	    	this._refreshFilter();
	    	
	    },
	    /**
	     * 删除过滤
	     * @param {String} text 要删除的过滤
	     * @param {Boolean} done 完成操作后是否刷新
	     */
	    delFilter:function(text, done) {
	    	if(typeof text === "string") {
	    		if(this.filter[text] != null) {
	    			delete this.filter[text];
	    		} else {
	    			return;
	    		}
	    	} else if(this.isArray(text)) {
	    		for(var i = 0, len = text.length; i < len; i++) {
	    			if(this.filter[text] != null) {
	    				delete this.filter[text];
	    			}
	    		}
	    	}
	    	this.filtered = false;
	    	for(var key in this.filter) {
	    		this.filtered = true;
	    		break;
	    	}
	    	if(done != false) {
	    	    this._refreshFilter();
	    	}
	    },
	    /**
	     * 过滤条件是否存在
	     * @param {string} text 过滤条件
	     * @return {Number | Boolean} 如果未传入要统计的过滤条件，则返回Number,否则，返回Boolean
	     */
	    filterExist:function(text) {
	    	if(text == null) {
	    		var fc = 0;
	    		for(key in this.filter) {
	    			fc ++;
	    		}
	    		return fc;
	    	} else {
	    	    return this.filter[text] != null;
	    	}
	    },
	    /**
	     * 清除过滤
	     * 
	     */
	    clearFilter:function() {
	    	this.filtered = false;
	    	this.filter = {};
	    	this._refreshFilter();
	    },
	    /**
	     * 初始化列表
	     * @param {Array} datas 列表数组
	     * @return {Boolean} 返回状态
	     */
	    setItemList:function(datas){
	    	this._maxLen = 0;
	    	if(this.isArray(datas) && datas.length > 0) {	    		
	    		if(typeof datas[0] == "string" || typeof datas[0] == "number") {
	    			var len = datas.length;
	    			this.datas = [];
	    			for(var i = 0; i < len; i++) {
	    				var cv = {};
	    				cv[this.textColName] = datas[i];
	    				this.datas.push(cv);
	    			}
	    		} else {
	    		    this.datas = datas;
	    		}
	    	} else {
			    this.filtered = false;
			    this.filter = {};
			    this.datas = [];
	    	}
	    	this.selectIndex = -1;
	    	if(this.datas.length > 0) {
	    		this.selectIndex = 0;
	    	}
	    	//同步过滤
	    	this._refreshFilter();
	    	return true;
	    },
	    /**
	     * 返回原始数据列表
	     * @param {String} filter
	     * @returns {Array} 数组
	     */
	    getItemList:function(filter) {
	    	if(filter === true) {
	    		return this.datas;
	    	} else {
	    	    return this.fDatas;
	    	}
	    },
	    /**
	     * 添加节点.
	     * @param {Object} item 节点json
	     * @return {Object} 返回状态
	     */
	    addItem:function(item) {
	    	this._maxLen = 0;
	    	if(typeof item == "string") {
	    		this.datas[this.datas.length][this.textColName] = item;
	    	} else if(typeof item == "object"){
	    		this.datas[this.datas.length] = item;
	    	}
	    	this.selectIndex = this.datas.length - 1;
	    	return true;
	    },
	    /**
	     * 删除节点.
	     * @param {Number} index 节点序号
	     * @return {Boolean} 返回状态
	     */
	    delItem:function(index) {
	    	if(this.filtered) {
	    		return false;
	    	}
	    	this._maxLen = 0;
	    	if(index >= 0 && index < this.datas.length) {
	    		this.datas.splice(index, 1);
	    	}
	    	if(this.datas.length > 0) {
		    	if(index > 0) {
		    	    this.selectIndex = index - 1;
		    	} else {
		    		this.selectIndex = index;
		    	}
	    	}
	    	return true;
	    },
	    /**
	     * 插入节点.
	     * @param {Number} index 节点序号
	     * @param {Object} item 节点对象/值
	     * @return {Boolean} 返回状态
	     */
	    insertItem:function(index, item) {
	    	if(this.filtered) {
	    		return false;
	    	}
	    	this._maxLen = 0;
	    	if(index >= 0 && index < this.datas.length) {
	    		var ds = item;
	    		if(typeof item == "string") {	    			
	    			ds = {};
	    			ds[this.textColName] = item;
	    		}
	    		this.datas.splice(index, 0, ds);
	    	}
	    	this.selectIndex = index;
	    	return true;
	    },
	    /**
	     * 修改节点.
	     * @param {Number} index 节点序号
	     * @param {Object} item 节点对象/值
	     * @return {Boolean} 返回状态
	     */
	    modItem:function(index, item) {
	    	if(this.filtered) {
	    		return false;
	    	}
	    	this._maxLen = 0;
	    	if(index >= 0 && index < this.datas.length) {
	    		var ds = item;
	    		if(typeof item == "string") {
	    			ds = {};
	    			ds[this.textColName] = item;
	    		}
	    		this.datas.splice(index, 1, ds);
	    	}
	    	return true;
	    },
	    /**
	     * 获取节点.
	     * @param {Number} index 节点序号
	     * @returns {Object} 返回节点
	     */
	    getItem:function(index, filter) {
	    	var curData = this.getItemList(filter);
	    	if(index >= 0 && index < curData.length) {
	    		return curData[index];
	    	}
	    	return null;
	    },
	    /**
	     * 定位到第一个节点.
	     * @param {String} filter 过滤条件
	     */
	    first:function(filter){
	    	var curData = this.getItemList(filter);
	    	if(curData.length > 0) {
	    	    this.selectIndex = 0;
	    	}
	    },
	    /**
	     * 定位到最后一个节点
	     * @param {String} filter 过滤条件
	     */
	    last:function(filter){
	    	var curData = this.getItemList(filter);
	    	if(curData.length > 0) {
	    		this.selectIndex = curData.length - 1;
	    	}
	    },
	    /**
	     * 定位到下一个节点.
	     * @param {String} filter 过滤条件
	     */
	    next:function(filter){
	    	var curData = this.getItemList(filter);
	    	if(this.selectIndex < curData.length - 1) {
	    		this.selectIndex ++ ;
	    	}
	    },
	    /**
	     * 定位到上一个节点.
	     */
	    prior:function() {
	    	if(this.selectIndex > 0) {
	    		this.selectIndex --;
	    	}
	    },
	    /**
	     * 长度
	     * @param {String} filter 过滤条件
	     */
	    length:function(filter){
	    	var curData = this.getItemList(filter);
	    	return curData.length;
	    },
	    /**
	     * 判断是否包含某字符串.
	     * @param {String} text 字符串
	     * @example 
	     *    list.indexOfText("123");
	     * @return {Number} 返回状态
	     */
	    indexOfText:function(text, filter) {
	    	var ri = -1;
	    	var curData = this.getItemList(filter);
	    	len = curData.length
	    	for(var i = 0; i < len; i++) {
	    		var ctext = curData[i][this.textColName] + "";
	    		if(this.trim(ctext) === this.trim(text)) {
	    			ri = i;
	    			break;
	    		}
	    	}
	    	curData = null;
	    	return ri;
	    },
	    /**
	     * 判断是否包含某值.
	     * @param {String | Number} value 值
	     * @param {String} filter 过滤条件
	     * @example
	     *    list.indexOf("123");
	     * @return {Number}
	     */
	    indexOf:function(value, filter) {
	    	var ri = -1;
	    	var curData = this.getItemList(filter);
	    	len = curData.length
	    	for(var i = 0; i < len; i++) {
	    		if(curData[i][this.idColName] != null) {
	    			if(curData[i][this.idColName] === value) {
	    			    ri = i;
	    			    break;
	    			}
	    		} else {
	    			if(curData[i][this.textColName] === value) {
	    			    ri = i;
	    			    break;
	    			}
	    		}
	    	}
	    	return ri;
	    },
	    /**
	     * 获取最长字符串.
	     * @param {String} filter过滤条件
	     * @return {String} 最长字符串
	     */
	    getMaxText:function(filter) {
	    	if(this._maxLen == 0) {
		    	var dl = this.getItemList(filter);
		    	for(var i = 0, len = dl.length; i < len; i++) {
		    		slen = this.strLen(dl[i][this.textColName] + "");
		    		if(this._maxLen < slen) {
		    			this._maxLen = slen;
		    			this._maxText = dl[i][this.textColName] + "";
		    		}
		    	}
		    	dl = null;
	    	}
	    	return this._maxText;
	    }
	});
	return qzz.list;
});
	